﻿using System.Text;
using Volo.Abp.DependencyInjection;
using YAPACS.BusinessObjects.Exams;
using YAPACS.RisServer.Abstractions;
using YAPACS.Workflows;

namespace YAPACS.RisServer.Workflows.Technician;

public class Call : WorkflowBase, ICall, ITransientDependency
{
    private readonly ICallQueuingService _callQueuingService;
    private readonly IExamEventService _examEventService;

    public Call(ICallQueuingService callQueuingService, IExamEventService examEventService)
    {
        _callQueuingService = callQueuingService;
        _examEventService = examEventService;
    }

    protected override async Task<WorkflowOutput> InvokeCoreAsync(WorkflowInput input)
    {
        const string key = "Exam";
        if (input.ContainsKey(key) && input.GetValueOrDefault(key) is Exam exam)
        {
            //调用接口等一系列操作
            await _callQueuingService.AddCallQueuingAsync(exam.ExamQueuing);
            exam.ExamQueuing.Status = ExamQueuingStatus.Ready;

            if (input.ContainsKey("Exams") && input.GetValueOrDefault("Exams") is List<Exam> exams)
            {
                if (exams.Count > 1)
                {
                }
            }

            if (input.ContainsKey("CallNext"))
            {
                var sex = exam.Patient.Sex switch
                {
                    PatientSex.Male => "男",
                    PatientSex.Female => "女",
                    PatientSex.Other => "不详",
                    _ => ""
                };

                var sb = new StringBuilder();
                sb.Append("<div class=\"calling-patient-info\">");
                sb.Append($"<div>编号：{exam.ExamQueuing.CallNumber}</div>");
                sb.Append($"<div>姓名：{exam.Patient.Name}</div>");
                sb.Append($"<div>性别：{sex}</div>");
                sb.Append($"<div>年龄：{exam.Age}</div>");
                sb.Append($"<div>项目：{exam.ExamItemsText}</div>");
                sb.Append("</div>");

                await _examEventService.AddExamEventAsync(exam, "呼叫患者");

                return new WorkflowOutput
                {
                    Success = true,
                    Info = sb.ToString()
                };
            }
        }
        return new WorkflowOutput()
        {
            Success = true
        };
    }
}
